package com.liuxinlong.modules.log.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.log.controller.request.QueryOperateLogReq;
import com.liuxinlong.modules.log.service.UserLogService;
import com.liuxinlong.push.MessagePushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户操作日志控制层
 * 
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2021-12-31
 */
@RestController
@RequestMapping("/log/operate")
public class OperateLogController {
	
	@Autowired
	private UserLogService userLogService;

	@Autowired
	private MessagePushService messagePushService;
	
	/**
	 * 分页查询用户操作日志
	 * 
	 * @param req 查询信息
	 * @return 用户日志分页数据
	 */
	@RequestMapping("page")
//	@LogAnnotation(value ="分页查询",business = LogBusinessConstants.OPERATE_LOG,operateType = LogOperateConstants.OPERATE_QUERY)
	public BaseResp pageUserLog(@RequestBody QueryOperateLogReq req) {
		Map<String,Object> queryMap = new HashMap<>();
		queryMap.put("business", req.getBusiness());
		queryMap.put("operateUser", req.getOperateUser());
		queryMap.put("startTime", req.getStartTime());
		queryMap.put("endTime", req.getEndTime());
		queryMap.put("content", req.getContent());
		queryMap.put("startNum", req.getStartNum());
		queryMap.put("pageSize", req.getPageSize());
		List<Map<String,Object>> dataList = userLogService.pageUserLog(queryMap);
		int total = userLogService.getUserLogCount(queryMap);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("分页查询用户运行日志信息成功");
		resp.setDataList(dataList);
		resp.setTotalNum(total);
		return resp;
	}

	/**
	 * 导出库存信息
	 *
	 * @param req      请求体
	 * @param response 返回体
	 */
	@PostMapping("export")
	@LogAnnotation(value = "导出操作日志", business = LogBusinessConstants.OPERATE_LOG,operateType = LogOperateConstants.OPERATE_EXPORT)
	public void exportUserLog(@RequestBody QueryOperateLogReq req, HttpServletResponse response) {
		Map<String, Object> queryParam = new HashMap<>();
		queryParam.put("business", req.getBusiness());
		queryParam.put("operateUser", req.getOperateUser());
		queryParam.put("startTime", req.getStartTime());
		queryParam.put("endTime", req.getEndTime());
		userLogService.exportUserLog(queryParam, response);
	}
}
